set matsize 10000
set more off

	drop p_j
	gen PRICE	=	sv/sq
	gen p_j		=	ln(PRICE)
	bysort pnic2 UNIT2 year : egen PRICE_mean = mean(PRICE)
	gen p_jmean 	=	ln(PRICE_mean)
	gen p_j2		= p_j^2
	gen p_j3		= p_j^3

	bysort pnic4 year: egen totsales4d = sum(sv)
	gen marketshare	=	sv/totsales4d
	replace msq		=	marketshare
		drop marketshare
	bysort pnic2 UNIT2 year	: egen ms_mean	= mean(msq)
	label var ms_mean "average ms by industry/year/unit"
	gen lxmfg		=	ln(xmfgshare)
		drop xmfgshare
	replace m		=	m_s
	gen msdif		=	msq - ms_mean
		drop totsales4d  
*** MTB: CHANGE IN ORIGINAL CODE: EXPORT DUMMY "EXP" DEFINED OUTSIDE THE ESTIMATION PROCEDURE		
*	gen exp = s173~=0
*	replace exp = 0 if s173==.
*		drop s173

* create translog terms:
	gen k2		=	k^2
	gen l2		=	l^2
	gen k3		=	k^3
	replace m3	=	m^3
	replace m2	=	m^2
	gen lk		=	l*k
	gen lm		=	l*m
	gen mk		=	m*k
	gen lmk		=	l*m*k

* selection correction [probability of remaining single product]:
	*put this to prevent "no convergence" in selection model for bootstrap
	if $dirnum!=0 {
		local iteratemax = ",iterate(2000)"
		}
	if $dirnum==0 {
		local iteratemax = ""
		}

*** MTB: CHANGE IN ORIGINAL CODE: TARRIFS DROPPED AND ADDITIONAL REGRESSORS ADDED + CHANGE FROM PROBIT TO LMP TO PREVENT CONVERGENCE PROBLEMS FOR SOME PRODUCT CATEGORIES
*	xi: probit single_next l_s* m* k_s* p_qt p_tf p_j* ms* exp i.year `iteratemax'		
	xi: reg single_next l_s* m* k_s* p_j* ms* exp i.year exp_p dest_p dest_p_sq treated_p exp dest dest_sq treated `iteratemax'	
	predict ProbSINGLE	

*identify firms with at least 2 sequences
	tsset fid year
	tsspell fid
	by fid: egen maxspell = max(_seq)
	drop if maxspell==1
	drop maxspell

xtset fup12 year, yearly

* interaction variable input price control function
	gen p_ms		=	p_j*msq
	gen pricel		=	p_j*l
	gen pricem		=	p_j*m
	gen pricek		=	p_j*k
	gen pricel2		=	p_j*l2
	gen pricem2		=	p_j*m2
	gen pricek2		=	p_j*k2
	gen pricelm		=	p_j*l*m
	gen pricelk		=	p_j*l*k
	gen pricemk		=	p_j*m*k
	gen pricelmk	=	p_j*l*m*k

	foreach v of varlist price* {
		gen lag`v'	= L.`v'
		drop `v'
	}

	
	xi, pref(A):	reg q_j i.UNIT2
	xi, pref(G):	reg q_j i.pcode6
	
	gen time	= year - 2001 //1988
	gen time2	= time^2
	
	
*translog
*1 baseline model:
*** MTB: CHANGE IN ORIGINAL CODE: TARRIFS COMMENTED OUT AND ADDITIONAL REGRESSORS ADDED
	global exog1 k k2 exp /*p_qt*/ time time2 A* G*  L.k L.k2 L.k3 L.p_j2 L.p_j3 /*L.p_qt L.p_tf*/ L.ProbSINGLE exp_p dest_p dest_p_sq treated_p dest dest_sq treated
	global endog1   m m2 l l2 lk lm mk lmk p_j p_j2 msq p_ms lxmfg
	global instr1 L.m L.m2 L.l L.l2 L.lk L.lm L.mk L.lmk L.p_j L.p_j2 L.msq L.p_ms L.exp L.lxmfg lagprice* L.exp_p L.dest_p L.dest_p_sq tdcinitiative_p L.dest L.dest_sq tdcinitiative
	ivreg2 q_j ${exog1} (${endog1}  = ${instr1}), gmm2s 
		gen nrobs1_st		=	e(N)
		gen beta1_l		=	_b[l]
		gen beta1_m		=	_b[m]
		gen beta1_k		=	_b[k]
		gen beta1_ll		=	_b[l2]
		gen beta1_mm		=	_b[m2]
		gen beta1_kk		=	_b[k2]
		gen beta1_lm		=	_b[lm]
		gen beta1_lk		=	_b[lk]
		gen beta1_mk		=	_b[mk]
		gen beta1_lmk		=	_b[lmk]
		
		gen delta1_1		=	_b[p_j]
		gen delta1_2		=	_b[p_j2]
		gen delta1_3		=	_b[msq]
		gen delta1_4		=	_b[p_ms]
*		gen delta1_5		=	_b[p_qt]
		gen delta1_5		=	_b[exp_p]
		gen delta1_6		=	_b[dest_p]
		gen delta1_7		=	_b[dest_p_sq]
		gen delta1_8		=	_b[treated_p]
		
		forvalues i=1/8 {
			gen gamma1_`i'	=	-delta1_`i'/(_b[l]+_b[m]+_b[k])
		}
	est clear		

*** MTB: CHANGE IN ORIGINAL CODE: NO INTEREST IN RESTRICTED MODELS	
/*	
*2: model without input price correction
	global exog2  k k2 time time2 L.k L.k2 L.k3 L.p_qt L.p_tf
	global endog2   m m2 l l2 lk lm mk lmk  
	global instr2 L.m L.m2 L.l L.l2 L.lk L.lm L.mk L.lmk   
	ivreg2 q_j ${exog2} (${endog2}  = ${instr2}), gmm2s 
		gen nrobs2_st		=	e(N)
		gen beta2_l		=	_b[l]
		gen beta2_m		=	_b[m]
		gen beta2_k		=	_b[k]
		gen beta2_ll		=	_b[l2]
		gen beta2_mm		=	_b[m2]
		gen beta2_kk		=	_b[k2]
		gen beta2_lm		=	_b[lm]
		gen beta2_lk		=	_b[lk]
		gen beta2_mk		=	_b[mk]
		gen beta2_lmk		=	_b[lmk]
		est clear

*2: model without selection correction
	global exog3 k k2 exp p_qt time time2 A* G*  L.k L.k2 L.k3 L.p_j2 L.p_j3 L.p_qt L.p_tf
	global endog3   m m2 l l2 lk lm mk lmk p_j p_j2 msq p_ms lxmfg
	global instr3 L.m L.m2 L.l L.l2 L.lk L.lm L.mk L.lmk L.p_j L.p_j2 L.msq L.p_ms L.exp L.lxmfg lagprice*
		ivreg2 q_j ${exog3} (${endog3}  = ${instr3}), gmm2s 
		gen nrobs3_st		=	e(N)
		gen beta3_l		=	_b[l]
		gen beta3_m		=	_b[m]
		gen beta3_k		=	_b[k]
		gen beta3_ll		=	_b[l2]
		gen beta3_mm		=	_b[m2]
		gen beta3_kk		=	_b[k2]
		gen beta3_lm		=	_b[lm]
		gen beta3_lk		=	_b[lk]
		gen beta3_mk		=	_b[mk]
		gen beta3_lmk		=	_b[lmk]
		est clear
		
	mac drop exog1 exog2 exog3 endog1 endog2 instr1 instr2 
